package cn.zfs.datasource.util;


import org.apache.commons.lang3.StringUtils;

import java.io.DataOutputStream;
import java.io.IOException;

@SuppressWarnings("JavaDoc")
public class DataSourceUtil {

    public static String getOrSet(String str, String connector, boolean isGet) {
        str = nameFirst2Big(str, connector);
        StringBuilder strBak = new StringBuilder();
        if (isGet) {
            strBak.append("get");
        } else {
            strBak.append("set");
        }
        strBak.append(str.substring(0, 1).toUpperCase()).append(str.substring(1));
        return strBak.toString();
    }

    /**
     * 类命名
     *
     * @param str
     * @return
     */
    public static String nameFirst2Big(String str, String connector) {
        StringBuilder strBak = new StringBuilder();
        String[] tbaleNames = str.split(connector);
        for (String tbaleName : tbaleNames) {
            strBak.append(tbaleName.substring(0, 1).toUpperCase()).append(tbaleName.substring(1));
        }
        return strBak.toString();
    }

    /**
     * 属性命名
     *
     * @param str
     * @return
     */
    public static String nameTwo2Big(String str, String connector) {
        StringBuilder strBak = new StringBuilder();
        String[] tbaleNames = str.split(connector);
        boolean flag = true;
        for (String tbaleName : tbaleNames) {
            if (flag) {
                flag = false;
                strBak.append(tbaleName);
                continue;
            }
            strBak.append(tbaleName.substring(0, 1).toUpperCase()).append(tbaleName.substring(1));
        }
        return strBak.toString();
    }

    /**
     * 类型转换
     * @param sqlType
     * @param encapsulation
     * @return
     */
    public static String convertSqlTypeToJavaType(String sqlType, boolean encapsulation) {
        if ("bit".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Boolean" : "boolean";
        } else if ("tinyint".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Byte" : "byte";
        } else if ("smallint".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Short" : "short";
        } else if ("int".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Integer" : "int";
        } else if ("bigint".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Long" : "long";
        } else if ("float".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Float" : "float";
        } else if ("decimal".equalsIgnoreCase(sqlType) || "numeric".equalsIgnoreCase(sqlType)
                || "real".equalsIgnoreCase(sqlType) || "money".equalsIgnoreCase(sqlType)
                || "smallmoney".equalsIgnoreCase(sqlType) || "double".equalsIgnoreCase(sqlType)) {
            return encapsulation ? "Double" : "double";
        } else if ("varchar".equalsIgnoreCase(sqlType) || "char".equalsIgnoreCase(sqlType)
                || "nvarchar".equalsIgnoreCase(sqlType) || "nchar".equalsIgnoreCase(sqlType)
                || "text".equalsIgnoreCase(sqlType) || "longtext".equalsIgnoreCase(sqlType)) {
            return "String";
        } else if ("date".equalsIgnoreCase(sqlType) || "datetime".equalsIgnoreCase(sqlType) || "timestamp".equalsIgnoreCase(sqlType)) {
            return "Date";
        } else if ("image".equalsIgnoreCase(sqlType)) {
            return "Blod";
        }
        return null;
    }

    /**
     * 导包
     *
     * @param sqlType
     * @return
     */
    public static String convertSqlTypeToImportPackage(String sqlType) {
        if ("bit".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("tinyint".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("smallint".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("int".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("bigint".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("float".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("decimal".equalsIgnoreCase(sqlType) || "numeric".equalsIgnoreCase(sqlType)
                || "real".equalsIgnoreCase(sqlType) || "money".equalsIgnoreCase(sqlType)
                || "smallmoney".equalsIgnoreCase(sqlType) || "double".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("varchar".equalsIgnoreCase(sqlType) || "char".equalsIgnoreCase(sqlType)
                || "nvarchar".equalsIgnoreCase(sqlType) || "nchar".equalsIgnoreCase(sqlType)
                || "text".equalsIgnoreCase(sqlType) || "longtext".equalsIgnoreCase(sqlType)) {
            return "";
        } else if ("date".equalsIgnoreCase(sqlType) || "datetime".equalsIgnoreCase(sqlType)
                || "timestamp".equalsIgnoreCase(sqlType)) {
            return "import java.util.Date;";
        }
        return "";
    }

    /**
     * 注解方式
     *
     * @param content
     * @param notesNum
     * @return
     */
    public static String getNotes(String content, int notesNum) {
        StringBuilder notes = new StringBuilder();
        notes.setLength(0);
        switch (notesNum) {
            case 1:
                notes.append("\t//");
                notes.append(content);
                notes.append("\n");
                break;
            case 2:
                notes.append("\t/*");
                notes.append("\t");
                notes.append(content);
                notes.append("*/\n");
                break;
            case 3:
                notes.append("\t/**\n");
                notes.append("\t * ");
                notes.append(content);
                notes.append("\n");
                notes.append("\t */\n");
                break;
        }
        return notes.toString();
    }

    /**
     * 生成路径
     *
     * @param packagePath
     * @return
     */
    public static String packagePath(String packagePath) {
        if (StringUtils.isBlank(packagePath)) {
            return packagePath;
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (!packagePath.contains(".")) {
            return stringBuilder.append(packagePath).append("/").toString();
        }
        String[] split = packagePath.split("\\.");
        for (String s : split) {
            stringBuilder.append(s).append("/");
        }
        return stringBuilder.toString();
    }

    /**
     * 字符串拼接
     * @param str
     * @return
     */
    public static String strConnect(String... str) {
        if (str.length <= 0) {
            return "";
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : str) {
                if (s == null) {
                    continue;
                }
                stringBuilder.append(s);
            }
            return stringBuilder.toString();
        }
    }

    /**
     * 写一行,每个str之间加空格,最后换行
     *
     * @param dataOutputStream
     * @param str
     * @return
     */
    public static void write(DataOutputStream dataOutputStream, String... str) throws IOException {
        if (str == null || str.length < 1) {
            return ;
        }
        for (int i = 0; i < str.length; i++) {
            if (i != 0) {
                if (str[0].equals("\t")) {
                    dataOutputStream.write("\t".getBytes());
                } else {
                    dataOutputStream.write(" ".getBytes());
                }
            }
            dataOutputStream.write(str[i].getBytes());
        }
        dataOutputStream.write("\n".getBytes());
    }
}
